package com.zhumo.sz.common.utils;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author zhumo
 * @comment 常用时间转换工具
 */
public class DateFormatUtil {

    //年月日格式
    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter dtfFull = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    public static Long toTs(String dtStr, boolean isFull) {
        LocalDateTime localDateTime = null;
        if (!isFull) {
            dtStr = dtStr + " 00:00:00";
        }
        localDateTime = LocalDateTime.parse(dtStr, dtfFull);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }
    public static Long toTs(String dtStr) {
        return toTs(dtStr, false);
    }
    public static String toDate(Long ts) {
        Date dt = new Date(ts);
        LocalDateTime localDateTime =
                LocalDateTime.ofInstant(dt.toInstant(), ZoneId.systemDefault());
        return dtf.format(localDateTime);
    }
    public static String toYmdHms(Long ts) {
        Date dt = new Date(ts);
        LocalDateTime localDateTime =
                LocalDateTime.ofInstant(dt.toInstant(), ZoneId.systemDefault());
        return dtfFull.format(localDateTime);
    }

    /**
     * 判断未成年人
     */
    public static boolean isYoungPeople(String dateString,long nowYear){
        String birthYear = dateString.substring(0, 4);
        return (nowYear - Long.parseLong(birthYear)) <18;
    }

    public static boolean isOldPeople(String dateString,long nowYear){
        String birthYear = dateString.substring(0, 4);
        return (nowYear - Long.parseLong(birthYear)) > 50;
    }

    /**
     * 计算两个timestamp之间相差的描述
     */
    public static long getSecondsDifference(Timestamp t1, Timestamp t2) {
        long millisecondsDiff = t2.getTime() - t1.getTime();
        return millisecondsDiff / 1000; // 将毫秒差转换为秒
    }


}
